<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>用户列表</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <link rel="stylesheet" href="/static/layui/layui/css/layui.css" media="all" />
    <link rel="stylesheet" href="/static/layui/css/public.css" media="all" />
</head>
<body class="childrenBody">
<form class="layui-form">
    <blockquote class="layui-elem-quote quoteBox">
        <form class="layui-form">
            <div class="layui-inline">
                <div class="layui-input-inline">
                    <input type="text" class="layui-input searchVal" id="uname" placeholder="请输入账号" />
                </div>
                <div class="layui-input-block layui-inline">
                    <select id="deptSearch" lay-verify="required">
                        <option value="">请选择部门</option>
                    </select>
                </div>
                <a class="layui-btn search_btn" data-type="reload"  id="searchVal">搜索</a>
            </div>
            <shiro:hasPermission name="user:add">
                <div class="layui-inline">
                    <a class="layui-btn layui-btn-normal addUser_btn" >添加用户</a>
                </div>
            </shiro:hasPermission>

            <shiro:hasPermission name="user:deleteBatch">
                <div class="layui-inline">
                    <a class="layui-btn layui-btn-danger layui-btn-normal delAll_btn">批量删除</a>
                </div>
            </shiro:hasPermission>

        </form>
    </blockquote>
    <table id="userList" lay-filter="userList"></table>
    <!--操作-->
    <script type="text/html" id="userListBar">
        <shiro:hasPermission name="user:look">
            <a class="layui-btn layui-btn-xs  layui-btn-warm" lay-event="look">查看</a>
        </shiro:hasPermission>

        <shiro:hasPermission name="user:update">
            <a class="layui-btn layui-btn-xs  layui-btn-primary" lay-event="edit">修改</a>
        </shiro:hasPermission>

        <shiro:hasPermission name="user:delete">
            <a class="layui-btn layui-btn-xs layui-btn-danger" lay-event="del">删除</a>
        </shiro:hasPermission>

        <shiro:hasPermission name="user:updateRole">
            <a class="layui-btn layui-btn-xs  layui-btn-normal" lay-event="assign">分配角色</a>
        </shiro:hasPermission>

        <shiro:hasPermission name="user:updateStatus">
            <a class="layui-btn layui-btn-xs " lay-event="userAble">状态</a>
        </shiro:hasPermission>

    </script>
</form>
<script type="text/javascript" src="/static/layui/layui/layui.js"></script>
<script>
    layui.use(['form','layer','laydate','table','laytpl'],function(){
        var form = layui.form,
            layer = parent.layer === undefined ? layui.layer : top.layer,
            $ = layui.jquery,
            laytpl = layui.laytpl,
            table = layui.table,
            laydate=layui.laydate;

        //用户列表
        var tableIns = table.render({
            elem: '#userList',
            url : '/user/users',
            cellMinWidth : 95,
            page : true,
            height : "full-125",
            limits : [5,10,15,20,25],
            limit : 5,
            id : "userListTable",
            cols : [[
                {type: "checkbox", fixed:"left", width:50},
                {field: 'uname', title: '账号', width:120, align:"center"},
                {field: 'password', title: '密码', width:120, align:"center"},
                {field: 'realname', title: '真实姓名',width:150,align:'center'},
                {field: 'available', title: '用户状态', width:180, align:'center',templet:function(d){
                    return d.available == 1 ? "启用" : "禁用";
                }},
                {field: 'dept', title: '部门', align:'center',width:180,templet:function (d) {
                     return d.dept.deptname;
                }},
                {title: '操作', minWidth:255, templet:'#userListBar',fixed:"right",align:"center"}
            ]]
        });
        //获取dept数据
        $.ajax({
            url:"/user/selectDept",
            success:function (res) {
                for(var i=0;i<res.length;i++){
                    var op=$("<option></option>");
                    console.log(res[i].deptid);
                    op.attr('value',res[i].deptid);
                    op.append(res[i].deptname);
                    $("#deptSearch").append(op);
                }
                form.render('select');
            }
        });

        //搜索
        $("#searchVal").click(function () {
            var uname=$('#uname').val();
            var deptId=$('#deptSearch').val();
           tableIns.reload({
               where:{
                   uname:uname,
                   deptId:deptId
               }
           })
        })
        $(".addUser_btn").click(function () {
           var addUser= layui.layer.open({
                title:"添加用户",
                type: 2,
                content: '/page/sys/useradd.jsp',
                shade:0.3,
                anim:0,
                area:["70%","90%"],
                maxmin:true,
                success:function (res) {
                        console.log(res+"添加");
                        setTimeout(function(){
                            layui.layer.tips('点击此处返回用户列表', '.layui-layer-setwin .layui-layer-close', {
                                tips: 3
                            });
                        },1000)
                }
            });
        })
       //操作
        table.on('tool(userList)', function(obj){ //注：tool 是工具条事件名，test 是 table 原始容器的属性 lay-filter="对应的值"
            var data = obj.data; //获得当前行数据
            var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
            if(layEvent === 'look'){ //查看
                //do somehing
                  lookUser(data);
            } else if(layEvent === 'del'){ //删除
                layer.confirm('真的删除行么', function(index){
                    delUser(data.uid);
                    obj.del(); //删除对应行（tr）;的DOM结构，并更新缓存
                    layer.close(index);
                    //向服务端发送删除指令
                });
            } else if(layEvent === 'edit'){ //编辑
                //do something
                //同步更新缓存对应的值
                editUser(data);
            } else if(layEvent === 'assign'){

                assignRole(data.uid);
            }else if(layEvent=='userAble'){
                updateStatus(data)
            }
        });
        //查看
        function lookUser(data) {
            layui.layer.open({
                title:"查看用户",
                type: 2,
                content: '/page/sys/userLook.jsp',
                shade:0.3,
                anim:0,
                area:["70%","90%"],
                maxmin:true,
                success:function (layero, index) {
                    //少了这个就不能从父页面向子页面传值
                    var body = layui.layer.getChildFrame('body', index);
                    body.contents().find("#uname").val(data.uname);
                    body.contents().find("#password").val(data.password);
                    body.contents().find("#realname").val(data.realname);
                    body.contents().find("#birthday").val(data.birthday);
                    body.contents().find("#available").val(data.available);
                    body.contents().find("#rname").val(data.roles[0].rname);
                    //获取图片的路径
                    body.contents().find("#headimg").attr("src",data.headimg);
                    body.contents().find("#deptid").val(data.dept.deptname);
                    setTimeout(function(){
                        layui.layer.tips('点击此处返回用户列表', '.layui-layer-setwin .layui-layer-close', {
                            tips: 3
                        });
                    },1000)
                }
            });

        }
        $(".delAll_btn").click(function () {
            var checkStatus=table.checkStatus('userListTable');
            var data=checkStatus.data;
            var idArray=new Array();
            if(data.length>0){
                for(var i=0;i<data.length;i++){
                   idArray.push(data[i].uid);
                }
                console.log(idArray);
                $.ajax({
                    url:'/user/deleteBatch',
                    type:'GET',
                    data:{uids:idArray},
                    dataType:"JSON",
                    success:function (res) {
                        if(res.code==200){
                            //提示
                            layui.layer.msg(res.msg);
                            //刷新表格
                            tableIns.reload();
                        }
                    }
                });
            }else{
                layui.layer.msg("你至少要选择一行");
            }
        });
        //删除
        function  delUser(uid) {
            $.ajax({
                type:"get",
                url:"/user/deleteUser",
                data:{uid:uid},
                success:function (de) {
                    console.log(de);
                    if(de.code==200){
                        layui.layer.msg(de.msg);
                        tableIns.reload();
                    }else{
                        layui.layer.msg(de.msg);
                    };
                }
            });
        };
        //修改
        function editUser(data) {
            layui.layer.open({
                title:"修改用户",
                type: 2,
                content: '/page/sys/userEdit.jsp',
                shade:0.3,
                anim:0,
                area:["70%","90%"],
                maxmin:true,
                success:function (layero, index) {
                    //少了这个就不能从父页面向子页面传值
                    var body = layui.layer.getChildFrame('body', index);
                    body.contents().find("#uid").val(data.uid);
                    body.contents().find("#uname").val(data.uname);
                    body.contents().find("#password").val(data.password);
                    body.contents().find("#realname").val(data.realname);
                    body.contents().find("#birthday").val(data.birthday);
                    body.contents().find("#dept").val(data.dept.deptid);
                    //获取图片的路径
                    body.contents().find("#userImage").attr("src",data.headimg);


                    setTimeout(function(){
                        layui.layer.tips('点击此处返回用户列表', '.layui-layer-setwin .layui-layer-close', {
                            tips: 3
                        });
                    },1000)
                }
            });
        }

        //更改状态 启用禁用
        function updateStatus(data) {
            $.ajax({
                type:"get",
                url:"/user/status",
                data:{uid:data.uid,available:data.available}
            });
            tableIns.reload();
        }
        function assignRole(uid) {
            layui.layer.open({
                title:"分配角色",
                type: 2,
                content: '/page/sys/assignRole.jsp',
                shade:0.3,
                anim:0,
                area:["45%","70%"],
                maxmin:true,
                success:function (layero, index) {
                    //少了这个就不能从父页面向子页面传值
                    var body = layui.layer.getChildFrame('body', index);
                    body.contents().find("#uid").val(uid);
                    console.log(body)
                    setTimeout(function(){
                        layui.layer.tips('点击此处返回用户列表', '.layui-layer-setwin .layui-layer-close', {
                            tips: 3
                        });
                    },1000)
                }
            });
        };

    })

</script>
</body>
</html>